package com.bdth.mixapi.api.controller.fenli;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdth.mixapi.api.entity.*;
import com.bdth.mixapi.api.service.*;
import com.bdth.mixapi.api.vo.CommonList;
import com.bdth.mixapi.api.vo.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/5/7.
 */
@RestController
@RequestMapping("/fenli")
public class IndexController {

    @Autowired
    RdOnlinerateService rdOnlinerateService;

    @Autowired
    IndustrysmssndquantityService industrysmssndquantityService;

    @Autowired
    IndustrydevicequantityService industrydevicequantityService;

    @Autowired
    DevicequantityService devicequantityService;

    @Autowired
    DepproviceService depproviceService;

    @Autowired
    MonthdevicesService monthdevicesService;

    @Autowired
    MsglogService msglogService;

    @ApiOperation(value="企业总数", notes="")
    @RequestMapping(value = "rd/allusercount", method = RequestMethod.GET)
    public CommonResult<String> getAllUserCount(){
        String allusercount = "5252";
        return CommonResult.success(allusercount);
    }

    @ApiOperation(value="用户总数", notes="")
    @RequestMapping(value = "rd/allrdcount", method = RequestMethod.GET)
    public CommonResult<String> getAllRdCount(){
        String allrdcount = "20951";
        //查询有数据的最后一天
        QueryWrapper<Devicequantity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Devicequantity> list = devicequantityService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Devicequantity> queryWrapper = new QueryWrapper<>();

            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            List<Devicequantity> onlinerateList = devicequantityService.list(queryWrapper);
            allrdcount = String.valueOf(onlinerateList.get(0).getDeviceNum());
        }
        return CommonResult.success(allrdcount);
    }

    @ApiOperation(value="在线用户总数", notes="")
    @RequestMapping(value = "rd/onlinerdcount", method = RequestMethod.GET)
    public CommonResult<String> getOnlineRdCount(){
        String onlinerdcount = "13252";
        return CommonResult.success(onlinerdcount);
    }

    @ApiOperation(value="在线率", notes="")
    @RequestMapping(value = "rd/onlinerdlv", method = RequestMethod.GET)
    public CommonResult<String> getOnlineRdLv(){
        String onlinerdlv = "48.24%";
        return CommonResult.success(onlinerdlv);
    }

    @ApiOperation(value="昨日接收总数", notes="")
    @RequestMapping(value = "rd/readmsgcount", method = RequestMethod.GET)
    public CommonResult<String> getReadMsgCount(){
        String readmsgcount = "3252万";
        return CommonResult.success(readmsgcount);
    }

    @ApiOperation(value="昨日发送总数", notes="")
    @RequestMapping(value = "rd/sendmsgcount", method = RequestMethod.GET)
    public CommonResult<String> getSendMsgCount(){
        String sendmsgcount = "3252万";
        return CommonResult.success(sendmsgcount);
    }

    @ApiOperation(value="在线率统计", notes="")
    @RequestMapping(value = "rd/onlinetj", method = RequestMethod.GET)
    public CommonList<RdOnlinerate> getOnlinerate(){
        //查询有数据的最后一天
        QueryWrapper<RdOnlinerate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<RdOnlinerate> list = rdOnlinerateService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<RdOnlinerate> queryWrapper = new QueryWrapper<>();

            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.orderByDesc("OnlineRate");
            queryWrapper.last("limit 20");
            List<RdOnlinerate> onlinerateList = rdOnlinerateService.list(queryWrapper);
            return CommonList.success(onlinerateList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="行业通信量统计", notes="")
    @RequestMapping(value = "rd/industrysms", method = RequestMethod.GET)
    public CommonList<Industrysmssndquantity> getIndustrysms(){
        //查询有数据的最后一天
        /**
         * 动态统计
        QueryWrapper<Industrysmssndquantity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Industrysmssndquantity> list = industrysmssndquantityService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Industrysmssndquantity> queryWrapper = new QueryWrapper<>();

            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.orderByDesc("RevNum");
            List<Industrysmssndquantity> industrysmssndquantityList = industrysmssndquantityService.list(queryWrapper);
            return CommonList.success(industrysmssndquantityList);
        }else{
            return CommonList.fail("没有数据");
        }
         **/
        //静态统计
        List<Industrysmssndquantity> industrysmssndquantityList = new ArrayList<>();
        Industrysmssndquantity bean1 = new Industrysmssndquantity();
        bean1.setIndustryName("铁路运输");
        bean1.setPosNum(5023);
        bean1.setRevNum(15032);
        bean1.setSndNum(2360);
        industrysmssndquantityList.add(bean1);

        Industrysmssndquantity bean2 = new Industrysmssndquantity();
        bean2.setIndustryName("减震救灾");
        bean2.setPosNum(3650);
        bean2.setRevNum(2500);
        bean2.setSndNum(1035);
        industrysmssndquantityList.add(bean2);

        Industrysmssndquantity bean3 = new Industrysmssndquantity();
        bean3.setIndustryName("气象和环境");
        bean3.setPosNum(2650);
        bean3.setRevNum(1500);
        bean3.setSndNum(1035);
        industrysmssndquantityList.add(bean3);

        Industrysmssndquantity bean4 = new Industrysmssndquantity();
        bean4.setIndustryName("电力热力");
        bean4.setPosNum(2650);
        bean4.setRevNum(1500);
        bean4.setSndNum(1035);
        industrysmssndquantityList.add(bean4);

        Industrysmssndquantity bean5 = new Industrysmssndquantity();
        bean5.setIndustryName("电气机械和器材制造业");
        bean5.setPosNum(1650);
        bean5.setRevNum(800);
        bean5.setSndNum(535);
        industrysmssndquantityList.add(bean5);


        Industrysmssndquantity bean7 = new Industrysmssndquantity();
        bean7.setIndustryName("房屋建筑业");
        bean7.setPosNum(350);
        bean7.setRevNum(200);
        bean7.setSndNum(135);
        industrysmssndquantityList.add(bean7);

        Industrysmssndquantity bean8 = new Industrysmssndquantity();
        bean8.setIndustryName("其他行业");
        bean8.setPosNum(3501);
        bean8.setRevNum(2002);
        bean8.setSndNum(1352);
        industrysmssndquantityList.add(bean8);

        return CommonList.success(industrysmssndquantityList);
    }

    @ApiOperation(value="行业用户统计", notes="")
    @RequestMapping(value = "rd/industryuser", method = RequestMethod.GET)
    public CommonList<Industrydevicequantity> getIndustryuser(){
        //查询有数据的最后一天
        QueryWrapper<Industrydevicequantity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Industrydevicequantity> list = industrydevicequantityService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Industrydevicequantity> queryWrapper = new QueryWrapper<>();

            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.orderByDesc("DeviceNum");
            List<Industrydevicequantity> industrysmssndquantityList = industrydevicequantityService.list(queryWrapper);
            return CommonList.success(industrysmssndquantityList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="地区用户统计", notes="")
    @RequestMapping(value = "rd/dquser", method = RequestMethod.GET)
    public CommonList<Depprovice> getDquser(){
        //查询有数据的最后一天
        QueryWrapper<Depprovice> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Depprovice> list = depproviceService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Depprovice> queryWrapper = new QueryWrapper<>();

            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.orderByDesc("DeviceNum");
            List<Depprovice> industrysmssndquantityList = depproviceService.list(queryWrapper);
            return CommonList.success(industrysmssndquantityList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="用户增长曲线", notes="")
    @RequestMapping(value = "rd/monthdevices", method = RequestMethod.GET)
    public CommonList<Monthdevices> getmonthDevices(){
        //查询有数据的最后一天
        QueryWrapper<Monthdevices> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("yearmonth");
        wrapper.last("limit 12");
        List<Monthdevices> list = monthdevicesService.list(wrapper);
        return CommonList.success(list);
    }

    @ApiOperation(value="最新加入单位", notes="")
    @RequestMapping(value = "rd/util", method = RequestMethod.GET)
    public CommonList<RdOnlinerate> getutil(){
        QueryWrapper<RdOnlinerate> wrapper = new QueryWrapper<>();
        wrapper.eq("StatisticDate","20190507");
        wrapper.orderByDesc("DepId");
        wrapper.last("limit 12");
        List<RdOnlinerate> list = rdOnlinerateService.list(wrapper);
        return CommonList.success(list);
    }

    @ApiOperation(value="数据推送日志前N条")
    @RequestMapping(value = "msglog/list", method = RequestMethod.GET)
    public CommonList<Msglog> getList(@RequestParam int top){
        QueryWrapper<Msglog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit "+top);
        List<Msglog> list = msglogService.list(queryWrapper);
        return CommonList.success(list);
    }
}
